package cn.anlab.anappframe.demo.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.anlab.anappframe.BaseActivity;
import cn.anlab.anappframe.R;
import cn.anlab.anappframe.demo.bo.ChatBO;
import cn.anlab.anappframe.system.Event;
import cn.anlab.anappframe.util.AQ;

public class ChatDemoActivity extends BaseActivity {

	LinearLayout panel;
	EditText edtInput;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_chat);
		
		interest(handler, Event.RECEIVE_CHAT_MESSAGE);
		
		panel = (LinearLayout)findViewById(R.id.sms_list_panel);
		
		edtInput = (EditText)findViewById(R.id.editText1);
		edtInput.setText("Hello.");
		
		Button btnSend = (Button)findViewById(R.id.button1);
		btnSend.setOnClickListener(btnSendClick);
		
	}
	
	public void showSmsSended(String content) {
		View pane = getLayoutInflater().inflate(R.layout.demo_chat_sms_sended, null);
		TextView tv= (TextView)pane.findViewById(R.id.textView2);
		tv.setText(content);
		panel.addView(pane);
	}
	
	public void showSmsReceived(String content) {
		View pane = getLayoutInflater().inflate(R.layout.demo_chat_sms_received, null);
		TextView tv= (TextView)pane.findViewById(R.id.textView1);
		tv.setText(content);
		panel.addView(pane);
	}
	
	
	/**
	 * 点击事件
	 */
	private View.OnClickListener btnSendClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			String input = edtInput.getText().toString().trim();
			Map m = new HashMap();
			m.put("msg", input);
			m.put("userKey", "123456");
//			showLoading();
			showSmsSended(input);
			AQ.addToAsyncQueue(ChatBO.class, "sendSms", m);
		}
	};

	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 333:
				
				break;
			case Event.RECEIVE_CHAT_MESSAGE:		//收到消息后
				hideLoading();
				Map data = (Map)msg.obj;
				String text = (String)data.get("msg");
				showSmsReceived(text);
				break;
			default:
				break;
			}
		};
	};

	protected void onActivityResult(int requestCode, int resultCode, android.content.Intent data) {
		
	};


}
